package fab.msn 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.external.ExternalInterface;
	import net.babeuf.logger.EveryWhereLogger;
	/**
	 * ...
	 * @author damien pegoraro
	 */
	public class MSNDialog extends EventDispatcher
	{
		public static const APP_ID:String = ""	// id unique pour l'application. Utile seulement pour inviter d'autres joueurs depuis flash.
		
		public var userEmail:String;
		public var userName:String;
		public var remoteUserName:String=" ";
		public var remoteUserEmail:String;
		
		private var _isMaster:Boolean=true;
		
		private var fake:Boolean;
		private var msnFakeConnection:MSNFakeConnection;
		
		public function MSNDialog(fake:Boolean=false) 
		{
			this.fake = fake;
		}
		
		public function init()  : void
		{
			//MSNJeuSurf.traceTextField.text += "MSNDialog init" + fake + "\r";
			if ( fake )
			{
				msnFakeConnection = new MSNFakeConnection( "msndialog", callFunction );
				msnFakeConnection.addEventListener("connected", connected) ; 
			}
			else if ( ExternalInterface.available )
			{
				EveryWhereLogger.fatal("SETTING CALLBACK") ; 
				
				ExternalInterface.addCallback("callFlashFunction", callFunction);
				getIsMaster();
				getEmail() ; 
				connected();
			}
		}
		
		private function connected(e:Event=null):void 
		{
			dispatchEvent( new MSNEvent( MSNEvent.CONNECTED )) ; 
		}
		
		public function callFunction( actions : String ):void
		{
			//MSNJeuSurf.traceTextField.text += "callFunction "+actions;
			var data : Array  = actions.split( "|" ) ; 
			var action : String = data.shift() ; 
			
			//if ( action == "" ) return ; 
		
			handleSpecialActions(action, data) ; 			
			dispatchEvent( new MSNEvent( MSNEvent.RECEIVED, actions, action, data )) ; 
		}
		
		private function handleSpecialActions(action : String, params : Array = null ):void
		{
			if ( fake )
			{
				switch ( action ) 
				{
					//case "name" :  userEmail = params[0] ; break ; 
					//case "email" :  userEmail = params[0] ; break ; 
					//case "remotename" :  remoteUserName = params[0] ; break ; 
					//case "remoteemail" :  remoteUserEmail = params[0] ; break ; 
					//case "ismaster" : isMaster = msnFakeConnection.isMaster ; break ; 
					case "ping" : sendAction("pong") ; break ; 
					case "pong": dispatchEvent( new MSNEvent(MSNEvent.PONG) ) ; break ; 
				}	
			}
			else {
				switch ( action ) 
				{
					case "name" :  userName = params[0] ; break ; 
					case "email" :  userEmail = params[0] ; break ; 
					case "remotename" :  remoteUserName = params[0] ; break ; 
					case "remoteemail" :  remoteUserEmail = params[0] ; break ; 
					case "ismaster" : 
					_isMaster = params[0] == "true" ;
					break ; 
					case "ping" : EveryWhereLogger.fatal("PING HANDLER") ;  sendAction("pong") ; break ; 
					case "pong": EveryWhereLogger.fatal("PONG HANDLER") ;  dispatchEvent( new MSNEvent(MSNEvent.PONG) ) ; break ; 
				}						
			}

		}
		
		/*
		ExternalInterfaceSend : la fonction qui envoie des messages.
			on compose le message ailleurs dans le code, on formate la string et on balance :
			ex : 	var str:String = "changeDir|"+String(xpos)+"|"+String(ypos)+"|"+String(angle)+"|"+String(speed)
			ExternalInterfaceSend(str)
		*/
		public function sendAction(action : String, ...params ) : void 
		{
			var out : String = action ; 
			if ( params && params.length > 0 ) out += "|" + params.join("|") ;  			
			if ( fake )
			{
				msnFakeConnection.call( "jsFunc", out );	
			}
			else if ( ExternalInterface.available )
			{
				//MSNJeuSurf.traceTextField.text += "\rsendAction"+action+' '+params;
				ExternalInterface.call("jsFunc", out);			
			}
		}	
		
		
		public function ping() : void
		{
			sendAction("ping") ; 
		}
		
		public function get isMaster() : Boolean 
		{
			if ( fake ) return msnFakeConnection.isMaster;
			else return _isMaster;
		}
		
			
		
		public function getIsMaster() : void 
		{
			//MSNJeuSurf.traceTextField.text += "sendCommand(getIsMaster\r";
			sendCommand("getIsMaster");
		}
		
		public function getName():void 
		{
			sendCommand("getName");
		}
		
		public function getEmail():void 
		{
			sendCommand("getEmail");
		}
		
		public function getRemoteName():void 
		{
			sendCommand("getRemoteName");
		}
		
		public function getRemoteEmail():void 
		{
			sendCommand("getRemoteEmail");
		}
		
		public function closeWindow():void 
		{
			sendCommand("jsFuncClose");
		}
		
		private function sendCommand( command:  String ) : void
		{
			if ( fake )
			{
				msnFakeConnection.call( command );	
			}
			else if ( ExternalInterface.available )
			{
				ExternalInterface.call( command );			
			}
		}
	}

}